# coding=utf-8
import traceback

import D2Ext

from gui.CObject import CObject
from .Constants import *


class CWidget(CObject):
    SIGNAL_ONCLICK = 0

    def __init__(self, parent=None):
        super(CWidget, self).__init__(parent)
        self.id = exnull_t
        self.x = 0
        self.y = 0
        self.dx = 0
        self.dy = 0
        self.width = 0
        self.height = 0

        self.wAlign = ALIGN_LEFT
        self.hAlign = ALIGN_CENTER
        self.state = INVISIBLE

        self.movable = False
        self.being_pressed = False
        self.being_moved = False
        self.being_selected = False

        self.background = D2Color.WHITE
        self.opacity = D2CDrawModes.NORMAL
        self.hover_text = ""

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass

    def set_position(self, x, y):
        self.x = x
        self.y = y
        if self.parent:
            self.dx = self.parent.x + self.x
            self.dy = self.parent.y + self.y
        else:
            self.dx = self.x
            self.dy = self.y

    # def resize(self):
    #     if self.parent:
    #         self.dx = self.parent.x + self.x
    #         self.dy = self.parent.y + self.y
    #
    #     D2Ext.LogMsg("widget:resize:x=%s,y=%s,dx=%s,dy=%s" % (self.x, self.y, self.dx, self.dy))

    def position(self):
        return self.x, self.y

    def set_size(self, width, height):
        self.width = width
        self.height = height

    def size(self):
        return self.width, self.height

    def set_state(self, state):
        self.state = state

    def state(self):
        return self.state

    def set_opacity(self, opacity):
        self.opacity = opacity

    def opacity(self):
        return self.opacity

    def set_hover_text(self, hover_txt):
        self.hover_text = hover_txt

    def hover_text(self):
        return self.hover_text

    def set_background(self, color):
        self.background = color

    def background(self):
        return self.background

    def set_horizontal_align(self, align):
        self.wAlign = align
        if self.wAlign is not None:
            self.relocate()

    def horizontal_align(self):
        return self.wAlign

    def set_vertical_align(self, align):
        self.hAlign = align
        if self.hAlign is not None:
            self.relocate()

    def vertical_align(self):
        return self.hAlign

    def being_pressed(self):
        return self.being_pressed

    def relocate(self):
        if self.parent:
            parentx = self.parent.GetX()
            parenty = self.parent.GetY()
            parentw = self.parent.GetWidth()
            parenth = self.parent.GetHeight()

            if self.wAlign == ALIGN_CENTER:
                self.x = ((parentx + parentw + parentx) - self.width) / 2
            elif self.wAlign == ALIGN_RIGHT:
                self.x = parentx + parentw - self.width
            if self.hAlign == ALIGN_CENTER:
                self.y = ((parenty + parenth + parenty) - self.height) / 2
            elif self.hAlign == ALIGN_RIGHT:
                self.y = parenty + parenth - self.height
        else:
            if self.wAlign == ALIGN_CENTER:
                self.x = (D2Client.WindowWidth - self.width) / 2
            elif self.wAlign == ALIGN_RIGHT:
                self.x = (D2Client.WindowWidth - self.width)
            if self.hAlign == ALIGN_CENTER:
                self.y = (D2Client.WindowHeight - self.height) / 2
            elif self.hAlign == ALIGN_RIGHT:
                self.y = D2Client.WindowHeight - self.height

    def draw(self):
        try:
            D2Ext.View_DrawRectangle(self.dx, self.dy, self.dx + self.width, self.dy + self.height, 0xFF, 5)
        except Exception as e:
            log_msg(traceback.format_exc())

    def draw_hover(self):
        if self.being_selected and self.hover_text:
            D2Ext.View_SetFontSize(1)
            D2Ext.View_PrintString(self.hover_text, self.dx, self.dy - 10, 11, 0)

    def event(self, sender, wparam):
        if sender == WM_LBUTTONDOWN:
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                self.being_pressed = True
                D2Ext.LogMsg(u'down %s！' % self)
                return True
        elif sender == WM_LBUTTONUP:
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                if self.state == VISIBLE:
                    self.dispatch(self, self.SIGNAL_ONCLICK)
                self.being_pressed = False
                D2Ext.LogMsg(u'up %s！' % self)
                return True
        elif sender == WM_MOUSEMOVE:
            # D2Ext.View_PrintTextOnTop("mouse x=%s, mouse y=%s" % (D2Ext.View_GetMouseX(), D2Ext.View_GetMouseY()), 0)
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                self.being_selected = True
            else:
                self.being_selected = False
            if not (sender == WM_MOUSEMOVE and wparam & MK_LBUTTON):
                return False
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                self.being_pressed = True
            else:
                self.being_pressed = False
        return False

    def show(self):
        self.state = VISIBLE

    def hide(self):
        self.state = INVISIBLE

    def onSubscribeMsg(self, control, exmsg):
        return
